package com.sky.controller.admin;

import com.sky.annotation.ClearCache;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * sky-take-out
 * com.sky.controller.admin
 *
 * @author Ablecisi
 * @version 1.0
 * 2025/2/28
 * 星期五
 * 23:51
 */
@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
public class DishController {

    private final DishService dishService;

    @Autowired
    public DishController(DishService dishService) {
        this.dishService = dishService;
    }

    /**
     * 添加菜品
     * @param dishDTO 菜品信息
     * @return 添加结果
     */
    @PostMapping
    @ApiOperation("添加菜品")
    @ClearCache
    public Result<String> addDish(@RequestBody DishDTO dishDTO) {
        dishService.addDishWithFlavor(dishDTO);
        return Result.success("添加成功");
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO 查询条件
     * @return 查询结果
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult<DishVO>> pageQueryDish(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询：{}", dishPageQueryDTO);
        PageResult<DishVO> pageDishResult = dishService.pageQueryDish(dishPageQueryDTO);
        return Result.success(pageDishResult);
    }

    /**
     * 根据Id查询菜品
     * @param id 菜品ID
     * @return 返回菜品VO
     */
    @ApiOperation("根据Id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> selectDishById(@PathVariable Long id){
        log.info("根据id{}查询菜品",id);
        DishVO dishVO = dishService.selectDishById(id);
        return Result.success(dishVO);
    }

    /**
     * 删除菜品
     * @param ids 需要删除的菜品的Id <br>
     * 如果在起售中的菜品或者在套餐中，不能简单的删除
     * @return 是否删除成功
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    @ClearCache
    public Result<String> deleteDishByIds(@RequestParam List<Long> ids) {
        log.info("删除菜品：{}", ids);
        dishService.deleteDishByIds(ids); // 除了菜品还需要删除相关联的口味和关系表
        return Result.success();
    }

    /**
     * 修改菜品
     * @param dishDTO 菜品DTO
     * @return 是否成功
     */
    @PutMapping()
    @ApiOperation("修改菜品")
    @ClearCache
    public Result<String> updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品{}",dishDTO);
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    /**
     * 修改菜品状态
     * @param id 菜品id
     * @param status 菜品状态
     * @return 是否成功
     */
    @PostMapping("/status/{status}")
    @ApiOperation("修改菜品状态")
    @ClearCache
    public Result<String> updateDishStatus(@RequestParam Long id, @PathVariable Integer status){
        log.info("修改菜品状态：{}",id);
        dishService.updateDishStatus(id, status);
        return Result.success();
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId 分类id
     * @return 菜品列表
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> selectDishByCategoryId(@RequestParam Long categoryId){
        log.info("根据分类id查询菜品：{}",categoryId);
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        List<DishVO> dishVOS = dishService.selectDishByCategoryId(dish);
        return Result.success(dishVOS);
    }
}

